package cn.wolfcode.demo;


import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.List;

public class test {
    @Test
    public void testDemo(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);
    }
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpnm/leave.bpmn")
                .addClasspathResource("bpnm/leave.png")
                .name("请假流程").deploy();
        System.out.println(deployment.getId() + "流程部署ID");
        System.out.println("流程部署名称" + deployment.getName());
    }
    @Test
    public void testRun(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance instance = runtimeService.startProcessInstanceByKey("leave");
        System.out.println("流程定义的id" + instance.getProcessDefinitionId());
        System.out.println("流程实例的id" + instance.getId());
    }
    @Test
    public void testTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("leave")
                .taskAssignee("李四").list();
        for (Task task : taskList) {
            System.out.println("流程定义id"+task.getProcessDefinitionId());
            System.out.println("流程实例Id" + task.getParentTaskId());
            System.out.println("任务id" + task.getId());
            System.out.println("任务名字" + task.getName());
        }
    }
    @Test
    public void testTask02(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("leave")
                .taskAssignee("张三").list();
        for (Task task : taskList) {
            taskService.addComment(task.getId(),task.getProcessInstanceId(),task.getName()+"审批通过");
            taskService.complete(task.getId());
        }
    }
    @Test
    public void testHistoryTask(){
        String processInstanceId="2501";
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = processEngine.getHistoryService();
        TaskService taskService = processEngine.getTaskService();
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
                .activityType("userTask").processDefinitionId(processInstanceId).taskAssignee("张三")
                .finished().list();
        for (HistoricActivityInstance instance : list) {
            System.out.println("任务名称" + instance.getActivityName());
            System.out.println("任务开始时间" + instance.getStartTime());
            System.out.println("任务结束时间" + instance.getEndTime());
            System.out.println("任务耗费时间" + instance.getDurationInMillis());
            List<Comment> taskComments = taskService.getTaskComments(instance.getTaskId());
            if (taskComments.size()>0.) {
                System.out.println("审批批注" + taskComments.get(0).getFullMessage());
            }
        }
    }
}
